package com.init.bootframe.business.service.impl;

import com.init.bootframe.business.dao.ICustomerDao;
import com.init.bootframe.business.service.ICustomerService;
import com.init.bootframe.business.vo.CustomerVO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements ICustomerService {

    private Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class); //日志

    @Autowired
    private ICustomerDao customerDao; //客户dao


    /**
     * 客户 分页查询
     * @param customerVO
     * @return
     */
    @Override
    public JSONObject findCustomerList(CustomerVO customerVO) {
        JSONObject jsonObject = new JSONObject();
        // 查询集合数据总数
        int total = customerDao.findCustomerCount(customerVO);
        // 查询集合数据
        List<CustomerVO> productList = new ArrayList<>();
        if (total > 0) {
            productList = customerDao.findCustomerList(customerVO);
        }
        jsonObject.put("rows", productList);
        jsonObject.put("total", total);
        return jsonObject;
    }

    /**
     * 客户 list查询
     * @param customerVO
     * @return
     */
    @Override
    public List<CustomerVO> findCustomerLists(CustomerVO customerVO) {
        return customerDao.findCustomerList(customerVO);
    }

    /**
     * 详情查询
     * @param customerVO
     * @return
     */
    @Override
    public CustomerVO findCustomer(CustomerVO customerVO) {
        return customerDao.findCustomer(customerVO);
    }

    /**
     * 客户添加
     * @param customerVO
     * @return
     */
    @Override
    public Map<String, Object> addCustomer(CustomerVO customerVO) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            customerDao.addCustomer(customerVO);
            resultMap.put("result", Boolean.TRUE);
            resultMap.put("msg", "添加客户信息成功！");
            logger.info("添加客户信息成功！");
        } catch (Exception e) {
            resultMap.put("result", Boolean.FALSE);
            resultMap.put("msg", "添加客户信息失败！");
            logger.info("添加客户信息失败！" + e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
        return resultMap;
    }

    /**
     * 客户更新
     * @param customerVO
     * @return
     */
    @Override
    public Map<String, Object> updateCustomer(CustomerVO customerVO) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            customerDao.updateCustomer(customerVO);
            resultMap.put("result", Boolean.TRUE);
            resultMap.put("msg", "更新客户信息成功！");
            logger.info("更新客户信息成功！");
        } catch (Exception e) {
            resultMap.put("result", Boolean.FALSE);
            resultMap.put("msg", "更新客户信息失败！");
            logger.info("更新客户信息失败！" + e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
        return resultMap;
    }

    /**
     * 客户删除
     * @param customerVO
     * @return
     */
    @Override
    public Map<String, Object> deleteCustomer(CustomerVO customerVO) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            customerDao.deleteCustomer(customerVO);
            resultMap.put("result", Boolean.TRUE);
            resultMap.put("msg", "删除客户信息成功！");
            logger.info("删除客户信息成功！");
        } catch (Exception e) {
            resultMap.put("result", Boolean.FALSE);
            resultMap.put("msg", "删除客户信息失败！");
            logger.info("删除客户信息失败！" + e.getMessage());
            throw new RuntimeException(e.getMessage());
        }
        return resultMap;
    }
}
